#include <stdlib.h>
#include <ncurses.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <wait.h>

#include <string.h>

#define PROMPT "smart-shell->"


extern int errno;


typedef void (*sighandler_t)(int);
static char *my_argv[100], *my_envp[100];
static char *search_path[10];
void handle_signal(int signo)
{
	printf("QUER SAIR\n");
	fflush(stdout);
	refresh();                      /* Print it on to the real screen */
        endwin();                       /* End curses mode                 */
	exit(EXIT_SUCCESS);

}



int main(int argc, char *argv[], char *envp[])
{
	char ch;
	int i, fd;
	int linha = 0;
	int coluna = 0;
	char *lin_cmd = (char *)malloc(sizeof(char) * 256);
	char *path_str = (char *)malloc(sizeof(char) * 256);
	char *cmd = (char *)malloc(sizeof(char) * 100);

	signal(SIGINT, SIG_IGN);
	signal(SIGINT, handle_signal);
	getcwd(path_str, 256);

	initscr();            /* Start curses mode            */

	clear();
	keypad(stdscr, TRUE); /* We get F1, F2 etc..          */
	noecho();             /* Don't echo() while we do getch */

	mvprintw(linha,coluna,"%s %s$", PROMPT, path_str);
	
	while(TRUE) {
		ch = getch();

	switch(ch){
		case '\n': 	if(lin_cmd[0] == '\0') {
					
					mvprintw(++linha,coluna,"%s %s$", PROMPT, path_str);
					printw("\nLINHA VASIA$\n");
				}
				else{
					
					/* parse and execute. */
					mvprintw(++linha,coluna,"%s %s$", PROMPT, path_str);
					printw("\nEXECUTANDO %s$\n", lin_cmd);
					bzero(lin_cmd, sizeof(lin_cmd)); 	//Limpa a linha de comando
				}
				
			break;
		case 02:	printw("\nSETA PARA BAIXO$\n");
				/*Percorre a lista de linhas de comando*/
			break;
		case 03:	printw("\nSETA PARA CIMA$\n");
				/*Percorre a lista de linhas de comando*/
			break;
		case 04:	printw("\nSETA PARA ESQUERDA$\n");
			break;
		case 05:	printw("\nSETA PARA DIREITA$\n");
			break;
		case 011:	printw("\nTAB PRECIONADO$\n");
			break;
		default: 	printw("%c",ch);
				strncat(lin_cmd,&ch,1);
				break;
		}

	}

	return 0;
}
